import { ElLoading, ElMessage, ElNotification } from 'element-plus'

/**
 * Element-plus - ElLoading
 * 打开： ElLoading.service({ lock: true, text, background: 'rgba(0, 0, 0, 0.7)' })
 * 关闭： ElLoadingInstance.close()
 */
let ElLoadingOpenCount = 0
let ElLoadingInstance = null

/**
 * 打开全屏 loading 框
 * @param {string} text
 */
export function openLoading(text = '加载中') {
  if (ElLoadingOpenCount <= 0) {
    ElLoadingInstance = ElLoading.service({
      lock: true,
      text,
      background: 'rgba(0, 0, 0, 0.7)'
    })
  }
  ElLoadingOpenCount++
}

/**
 * 关闭全屏 loading 框
 */
export function closeLoading() {
  ElLoadingOpenCount--
  if (ElLoadingOpenCount <= 0) {
    ElLoadingInstance?.close()
    // 重新初始化
    ElLoadingOpenCount = 0
    ElLoadingInstance = null
  }
}

/**
 * Element-plus - ElMessage
 * 消息提示框
 */

/**
 * 全局 错误提示
 * @param {string} text
 */
export function openErrorMessage(text) {
  if (!text) return
  ElMessage.error(text)
}
export function openSuccessMessage(text) {
  if (!text) return
  ElMessage.success(text)
}

export function openErrorNotification(message) {
  ElNotification({
    title: '错误提示',
    message,
    type: 'error'
  })
}
export function openSuccessNotification(message) {
  ElNotification({
    title: '成功提示',
    message,
    type: 'success'
  })
}
